package com.youxue.controller;

import com.alibaba.fastjson.JSONObject;
import com.youxue.entity.ClassType;
import com.youxue.entity.CorrectJsonObject;
import com.youxue.entity.ErrorJsonObject;
import com.youxue.service.ClassTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 15c on 2018/1/17.
 */
@Controller
@RequestMapping("/classtype")
public class ClassTypeController {
    @Autowired
    ClassTypeService classTypeService;

    /**
     * 用于分类选择显示
     * 返回课程根目录类型（如小学、初中、高中）
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/firsttype", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    String getFirstType() {

        List<ClassType> classTypeList = new ArrayList<ClassType>();
        classTypeList = classTypeService.showAllRootClassType();
        CorrectJsonObject correctJsonObject = new CorrectJsonObject("查询成功");
        correctJsonObject.setData(classTypeList);
        return JSONObject.toJSONString(correctJsonObject);

    }

    /**
     * 用于分类选择显示
     * 根据一级类pid查询二级目录(查询小学、初中、高中各包含的课程如：pid=0，typeName=“小学")；
     * 也可根据二级类pid查询三级目录(查询小学、初中、高中各包含的课程中所具有的内容：语文——任务介绍)；
     *
     * @param pid 父级id（=0）
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/secondtype", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    String getSecondtype(@RequestParam(required = false) Integer pid) {
        if (null == pid) {
            ErrorJsonObject errorJsonObject = new ErrorJsonObject(102, "参数有误");
            return JSONObject.toJSONString(errorJsonObject);
        }
        List<ClassType> classTypeList = new ArrayList<ClassType>();
        classTypeList = classTypeService.showAllSecondClassType(pid);
        CorrectJsonObject correctJsonObject = new CorrectJsonObject("查询成功");
        correctJsonObject.setData(classTypeList);
        return JSONObject.toJSONString(correctJsonObject);


    }
}
